import { createRouter, createWebHistory } from 'vue-router'
import { getAllData } from '@/apis/mysql'
import home from '../views/home/index.vue'
const modules = import.meta.glob('../views/**/*.vue');
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: home,
    }
  ]
})
router.beforeEach((to, from, next) => {
  if (to.name) {
    getAllData('descks').then(res => {
      res.data.map((x) => {
        router.addRoute({
          name: x.router,
          path: '/' + x.router,
          component: modules[`../views/${x.path}.vue`]
        })
      })
    })
  } else {
    next('/')
  }
  next()
})
export default router
